from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.by import By

from spider_h5.seleinum_bankend.pages.first_level.index_page import IndexPage
from spider_h5.seleinum_bankend.utils.code_analise import base64_api


class LoginPage(object):

    def __init__(self, client):
        self.client = client
        self.browser: WebDriver = client.browser

    def login(self, username, password):
        self.browser.get("http://pre.palmcatering.eu/admin")
        #  输入账号密码
        self.browser.find_element(By.XPATH, '//*[@id="my-form"]/div[1]/input').send_keys(username.strip())
        self.browser.find_element(By.XPATH, '//*[@id="my-form"]/div[2]/input').send_keys(password.strip())
        element = self.browser.find_element(By.CSS_SELECTOR, '#my-form > div.captcha-group > img')
        img_data = element.screenshot_as_base64
        code = base64_api(uname='RocklyLee', pwd='Adcbtug0189', img=img_data, typeid='1')
        self.browser.find_element(By.XPATH, '//*[@id="my-form"]/div[3]/div[1]/input').send_keys(code.strip())
        self.browser.find_element(By.XPATH, '//*[@id="my-form"]/div[4]/button').click()
        self.browser.implicitly_wait(10)
        return IndexPage(self.client)
